﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace BasicComponents.utils
{
    /// <summary>
    /// 自定义公式解析，用于解析window_definFormula窗口设置的公式
    /// </summary>
    public class DefinedFormulaUtils
    {

        /// <summary>
        /// 分割字符串
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        internal static List<String> spliter(String sourceStr, String splitStr, String appendSuffix, String appendPrefix)
        {
           // LogPrinter.LogPrinter.printModDebugLog2File("拆分公式(spliter方法),源字符串："+ sourceStr, "GSJieXi");
            List<String> list_gsNewStr = new List<string>();
            if (sourceStr.IndexOf(splitStr) < 0)
            {
              //  LogPrinter.LogPrinter.printModDebugLog2File("拆分公式(spliter方法),不存在分隔符，直接返回：" + sourceStr, "GSJieXi");
                list_gsNewStr.Add(sourceStr);
            }
            while (sourceStr.IndexOf(splitStr) >= 0)
            {
                list_gsNewStr.Add(sourceStr.Substring(0, sourceStr.IndexOf(splitStr)) + appendSuffix);
                sourceStr = (splitStr.EndsWith(appendPrefix)?appendPrefix:"")+sourceStr.Substring(sourceStr.IndexOf(splitStr) + splitStr.Length);
                if (sourceStr.IndexOf(splitStr) < 0)
                {
                 //   LogPrinter.LogPrinter.printModDebugLog2File("拆分公式(spliter方法),拆分结果：" + sourceStr, "GSJieXi");
                    list_gsNewStr.Add(sourceStr);
                }
            }
         //   LogPrinter.LogPrinter.printModDebugLog2File("拆分公式(spliter方法),返回公式列表长度：" + list_gsNewStr.Count, "GSJieXi");
            return list_gsNewStr;
        }
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <param name="splitStr"></param>
        /// <returns></returns>
        private static List<String> substringFiled(String sourceStr, String prefix, String suffix)
        {
            List<String> list_gsNewStr = new List<string>();
            
            while (sourceStr.IndexOf(prefix) >= 0 && sourceStr.IndexOf(suffix) >= 0 && (sourceStr.IndexOf(prefix) < sourceStr.IndexOf(suffix)))
            {
                LogPrinter.LogPrinter.printModDebugLog2File("截取字段原始内容：" + sourceStr + ",开始截取：" + (sourceStr.IndexOf(prefix) + prefix.Length) + ",截取长度：" + (sourceStr.IndexOf(suffix) - (sourceStr.IndexOf(prefix) + prefix.Length)), "GSJieXi");
                String tmpStr = sourceStr.Substring(sourceStr.IndexOf(prefix) + prefix.Length, sourceStr.IndexOf(suffix) - (sourceStr.IndexOf(prefix) + prefix.Length));
                if (!list_gsNewStr.Contains(tmpStr))
                {
                    list_gsNewStr.Add(tmpStr);
                }
                LogPrinter.LogPrinter.printModDebugLog2File("截取字段到内容："+tmpStr, "GSJieXi");
                sourceStr = sourceStr.Substring(sourceStr.IndexOf(suffix) + suffix.Length);
                LogPrinter.LogPrinter.printModDebugLog2File("截取字段时剩余内容：" + sourceStr, "GSJieXi");
            }
            return list_gsNewStr;
        }

        /// <summary>
        /// //统一进行公式的解析,解析方式采用javascript
        /// </summary>
        /// <param name="obj">传入实体，该实体包含gsnr中的所有字段</param>
        /// <param name="gsnr">公式内容（非显示的公式内容）</param>
        /// <param name="filedName">当前解析的字段名，主要用于日志打印，别无他用</param>
        /// <returns></returns>
        public static String analyseFormulaByJS(Object obj, String gsnr, String filedName)
        {
            String valResult = "";
           // LogPrinter.LogPrinter.printModDebugLog2File("即将解析字段" + filedName, "GSJieXi");
            String gsNewStr = gsnr;
            {
               // LogPrinter.LogPrinter.printModDebugLog2File("即将拆解字段！", "GSJieXi");
                //第一步拆解出公式中的字段值，并且替换。
                List<String> fileList = substringFiled(gsNewStr, "[F&", "&F]");
                //LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",拆解字段完成，即将查找字段值！", "GSJieXi");
                Dictionary<String, string> fileDic = new Dictionary<string, string>();
                //判断系统中字段是否都存在
                foreach (String str in fileList)
                {
                    PropertyInfo pro = obj.GetType().GetProperty(str);
                    if (pro == null)
                    {
                        throw new Exception("公式字段" + str + "未找到!");
                        LogPrinter.LogPrinter.printErrorLog2File("公式字段" + str + "未找到!", "GSJieXi");
                    }
                    String val = pro.GetValue(obj, null).ToString();
                    if (pro.PropertyType.Equals(new Decimal().GetType()) || pro.PropertyType.Equals(new float().GetType()) || pro.PropertyType.Equals(new int().GetType()))
                    {
                      //  LogPrinter.LogPrinter.printModDebugLog2File("字段" + str + "属于数值类型！", "GSJieXi");
                    } 
                    else
                    {
                      //  LogPrinter.LogPrinter.printModDebugLog2File("字段" + str + "属于字符类型！", "GSJieXi");
                        val = "'" + val + "'";
                    }
                    fileDic.Add(str, val);
                }
              //  LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",查找字段值完成！", "GSJieXi");
                //拆分公式列表 
                List<String> gsList = new List<string>();
                List<String> list_gs_tmp = DefinedFormulaUtils.spliter(gsNewStr, "&V];[F&", "&V]", "[F&");
                foreach (String str in list_gs_tmp)
                {
                    foreach (String str_tmp in DefinedFormulaUtils.spliter(str, "&V];:[V&", "&V]", ":[V&"))
                    {
                        gsList.Add(str_tmp);
                    }
                }
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",拆解公式列表完成！", "GSJieXi");
                StringBuilder sbForJs = new StringBuilder();
                //拆解替换公式列表信息
                for (int i = 0; i < gsList.Count; i++)
                {
                    String gsTmp = gsList[i];
                    //替换条件
                    gsTmp = gsTmp.Replace("[T&=&T]", "==");
                    gsTmp = gsTmp.Replace("[T&>&T]", ">");
                    gsTmp = gsTmp.Replace("[T&>=&T]", ">=");
                    gsTmp = gsTmp.Replace("[T&<&T]", "<");
                    gsTmp = gsTmp.Replace("[T&<=&T]", "<=");
                    gsTmp = gsTmp.Replace("[T&<>&T]", "!=");
                    while (gsTmp.Contains("[T&startWith&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&startWith&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])==0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&startWith&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&startWith&T]".Length);
                    }
                    while (gsTmp.Contains("[T&contains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&contains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])>=0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&contains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&contains&T]".Length);
                    }
                    while (gsTmp.Contains("[T&notContains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&notContains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])<0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&notContains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&notContains&T]".Length);
                    }
                    //替换长度内容
                    gsTmp = gsTmp.Replace("[T&length>=&T]", ".length >= ");
                    gsTmp = gsTmp.Replace("[T&length<=&T]", ".length <= ");
                    gsTmp = gsTmp.Replace("[T&length!=&T]", ".length != ");
                    //替换值内容
                    gsTmp = gsTmp.Replace("[C&", "");
                    gsTmp = gsTmp.Replace("&C]", "");
                    //替换结果值前缀
                    gsTmp = gsTmp.Replace("&L]:[V&", "&L]){[V&");
                    //替换逻辑条件
                    gsTmp = gsTmp.Replace("[L&)and(&L]", ") && (");
                    gsTmp = gsTmp.Replace("[L&)and&L]", ") && ");
                    gsTmp = gsTmp.Replace("[L&and(&L]", " && (");
                    gsTmp = gsTmp.Replace("[L&and&L]", " && ");
                    gsTmp = gsTmp.Replace("[L&)or(&L]", ") || (");
                    gsTmp = gsTmp.Replace("[L&)or&L]", ") || ");
                    gsTmp = gsTmp.Replace("[L&or(&L]", " || (");
                    gsTmp = gsTmp.Replace("[L&or&L]", " || ");
                    gsTmp = gsTmp.Replace("[L&)&L]", " ) ");
                    gsTmp = gsTmp.Replace("[L&&L]", " ");
                    //如果只有结果则直接添加if true
                    gsTmp = gsTmp.Replace(":[V&", "true){[V&");
                    //替换结果字符
                    gsTmp = gsTmp.Replace("[V&", " ");
                    gsTmp = gsTmp.Replace("&V]", " ");
                    //添加至整体字符串 
                    sbForJs.AppendLine((sbForJs.Length <= 0 ? "" : " else ") + "if(" + gsTmp + " }");
                }
                //最后替换系统字段  
                foreach (String key in fileDic.Keys)
                {
                    String val = "";
                    fileDic.TryGetValue(key, out val);
                    sbForJs.Replace("[F&" + key + "&F]", "" + val + "");
                }
                //最后添加else字段
                sbForJs.Append(sbForJs.Length > 0 ? " else {''}" : "");
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",替换及组合公式列表成js代码完成！", "GSJieXi");
                //LogPrinter.LogPrinter.printModDebugLog2File("解析新公式fileGSNew，公式原值：" + gsNewStr + ",解析后：" + sbForJs.ToString(), "GSJieXi");

                try
                {
                    valResult = Microsoft.JScript.Eval.JScriptEvaluate(sbForJs.ToString(), Microsoft.JScript.Vsa.VsaEngine.CreateEngine()).ToString();
                    LogPrinter.LogPrinter.printModDebugLog2File("JS解析后内容值：" + valResult, "GSJieXi");
                }
                catch (Exception e1)
                {
                    throw new Exception("JS解析公式（" + sbForJs + "）出错（" + e1.Message + "）");
                }
            }
           // LogPrinter.LogPrinter.printModDebugLog2File("完成解析字段：" + filedName, "GSJieXi");
            return valResult;
        }
        /// <summary>
        /// //统一进行公式的解析,解析方式采用javascript
        /// </summary> 
        /// <param name="obj">传入实体，该实体包含gsnr中的所有字段</param>
        /// <param name="gsnr">公式内容（非显示的公式内容）</param>
        /// <param name="filedName">当前解析的字段名，主要用于日志打印，别无他用</param>
        /// <returns></returns>
        public static String analyseFormulaByJS(Dictionary<String, String> resKeyVal, Dictionary<String, String> resKeyType, String gsnr, String filedName)
        {
            String valResult = "";
            LogPrinter.LogPrinter.printModDebugLog2File("即将解析字段" + filedName + "原始公式：" + gsnr, "GSJieXi");
           // LogPrinter.LogPrinter.printModDebugLog2File("字段类型表大小：" + resKeyType.Count , "GSJieXi");
            foreach (String key in resKeyType.Keys) {
                String val = "";
                resKeyType.TryGetValue(key, out val);
              //  LogPrinter.LogPrinter.printModDebugLog2File("字段:"+key+",类型:" + val, "GSJieXi");
            }

            String gsNewStr = gsnr;
            {
              //  LogPrinter.LogPrinter.printModDebugLog2File("即将拆解字段！", "GSJieXi");
                //第一步拆解出公式中的字段值，并且替换。
                List<String> fileList = substringFiled(gsNewStr, "[F&", "&F]");
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",拆解字段完成，即将查找字段值！", "GSJieXi");
                Dictionary<String, string> fileDic = new Dictionary<string, string>();
                //判断系统中字段是否都存在
                foreach (String str in fileList)
                {
                    if (!resKeyVal.Keys.Contains(str))
                    {
                        throw new Exception("公式字段" + str + "未找到!");
                        LogPrinter.LogPrinter.printErrorLog2File("公式字段" + str + "未找到!", "GSJieXi");
                    }
                    String val = "";
                    resKeyVal.TryGetValue(str, out val);
                    if (resKeyType != null && resKeyType.ContainsKey(str))
                    {
                        String typeStr = "";
                        resKeyType.TryGetValue(str, out typeStr);
                        if (typeStr.Equals("numeric", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("decimal", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("int", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("int16", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("int32", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("int64", StringComparison.CurrentCultureIgnoreCase) || typeStr.Equals("float", StringComparison.CurrentCultureIgnoreCase))
                        { 
                           // LogPrinter.LogPrinter.printModDebugLog2File("字段"+str+"属于数值类型！", "GSJieXi");
                        }
                        else
                        {
                         //   LogPrinter.LogPrinter.printModDebugLog2File("字段" + str + "属于字符类型！", "GSJieXi");
                            val = "'" + val + "'";
                        }
                    }
                    else
                    {
                     //   LogPrinter.LogPrinter.printModDebugLog2File("字段类型表中无当前字段"+str+",系统将默认为字符串处理！", "GSJieXi");
                        val = "'" + val + "'";
                    }
                    fileDic.Add(str, val);
                }
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",查找字段值完成！", "GSJieXi");
                //拆分公式列表
                List<String> gsList = new List<string>();
                List<String> list_gs_tmp = DefinedFormulaUtils.spliter(gsNewStr, "&V];[F&", "&V]", "[F&");
                foreach (String str in list_gs_tmp)
                {
                    foreach (String str_tmp in DefinedFormulaUtils.spliter(str, "&V];:[V&", "&V]", ":[V&"))
                    {
                        gsList.Add(str_tmp);
                      //  LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",拆解成单个公式列表:"+str_tmp, "GSJieXi");
                    }
                }
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",拆解公式列表完成！", "GSJieXi");
                StringBuilder sbForJs = new StringBuilder();
                //拆解替换公式列表信息
                for (int i = 0; i < gsList.Count; i++)
                {
                    String gsTmp = gsList[i];
                    //替换条件
                    gsTmp = gsTmp.Replace("[T&=&T]", "==");
                    gsTmp = gsTmp.Replace("[T&>&T]", ">");
                    gsTmp = gsTmp.Replace("[T&>=&T]", ">=");
                    gsTmp = gsTmp.Replace("[T&<&T]", "<");
                    gsTmp = gsTmp.Replace("[T&<=&T]", "<=");
                    gsTmp = gsTmp.Replace("[T&<>&T]", "!=");
                    while (gsTmp.Contains("[T&startWith&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&startWith&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])==0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&startWith&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&startWith&T]".Length);
                    }
                    while (gsTmp.Contains("[T&contains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&contains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])>=0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&contains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&contains&T]".Length);
                    }
                    while (gsTmp.Contains("[T&notContains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&notContains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])<0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&notContains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&notContains&T]".Length);
                    }
                    //替换长度内容
                    gsTmp = gsTmp.Replace("[T&length>=&T]", ".length >= ");
                    gsTmp = gsTmp.Replace("[T&length<=&T]", ".length <= ");
                    gsTmp = gsTmp.Replace("[T&length!=&T]", ".length != ");
                    //替换值内容
                    gsTmp = gsTmp.Replace("[C&", "");
                    gsTmp = gsTmp.Replace("&C]", "");
                    //替换结果值前缀
                    gsTmp = gsTmp.Replace("&L]:[V&", "&L]){[V&");
                    //替换逻辑条件
                    gsTmp = gsTmp.Replace("[L&)and(&L]", ") && (");
                    gsTmp = gsTmp.Replace("[L&)and&L]", ") && ");
                    gsTmp = gsTmp.Replace("[L&and(&L]", " && (");
                    gsTmp = gsTmp.Replace("[L&and&L]", " && ");
                    gsTmp = gsTmp.Replace("[L&)or(&L]", ") || (");
                    gsTmp = gsTmp.Replace("[L&)or&L]", ") || ");
                    gsTmp = gsTmp.Replace("[L&or(&L]", " || (");
                    gsTmp = gsTmp.Replace("[L&or&L]", " || ");
                    gsTmp = gsTmp.Replace("[L&)&L]", " ) ");
                    gsTmp = gsTmp.Replace("[L&&L]", " ");
                    //如果只有结果则直接添加if true
                    gsTmp = gsTmp.Replace(":[V&", "true){[V&");
                    //替换结果字符
                    gsTmp = gsTmp.Replace("[V&", " ");
                    gsTmp = gsTmp.Replace("&V]", " ");
                  //  LogPrinter.LogPrinter.printModDebugLog2File("解析字段" + filedName + ",替换单个公式列表:" + "if(" + gsTmp + " }", "GSJieXi");
                    //添加至整体字符串 
                    sbForJs.AppendLine((sbForJs.Length <= 0 ? "" : " else ") + "if(" + gsTmp + " }");
                }
                //最后替换系统字段  
                foreach (String key in fileDic.Keys)
                {
                    String val = "";
                    fileDic.TryGetValue(key, out val);
                    sbForJs.Replace("[F&" + key + "&F]", "" + val + "");
                }
                //最后添加else字段
                sbForJs.Append(sbForJs.Length > 0 ? " else {''}" : ""); 
               // LogPrinter.LogPrinter.printModDebugLog2File("解析新公式fileGSNew，公式原值：" + gsNewStr + ",解析后：" + sbForJs.ToString(), "GSJieXi"); 
                try
                {
                    valResult = Microsoft.JScript.Eval.JScriptEvaluate(sbForJs.ToString(), Microsoft.JScript.Vsa.VsaEngine.CreateEngine()).ToString();
                    LogPrinter.LogPrinter.printModDebugLog2File("JS解析后内容值：" + valResult, "GSJieXi");
                }
                catch (Exception e1)
                {
                    throw new Exception("JS解析公式（" + sbForJs + "）出错（" + e1.Message + "）");
                }
            }
           // LogPrinter.LogPrinter.printModDebugLog2File("完成解析字段：" + filedName, "GSJieXi");
            return valResult;
        }

        /// <summary>
        /// //统一进行公式拆解,不解析公式，只替换成需要显示的js格式代码
        /// </summary>
        /// <param name="obj">传入实体，该实体包含gsnr中的所有字段</param>
        /// <param name="gsnr">公式内容（非显示的公式内容）</param>
        /// <param name="filedName">当前解析的字段名，主要用于日志打印，别无他用</param>
        /// <returns></returns>
        public static String analyseFormula2JS(String gsnr, String filedName) {
            return chaijieGSByJS(gsnr, filedName);
        }

        /// <summary>
        /// //统一进行公式拆解,不解析公式，只替换成需要显示的js格式代码
        /// </summary>
        /// <param name="obj">传入实体，该实体包含gsnr中的所有字段</param>
        /// <param name="gsnr">公式内容（非显示的公式内容）</param>
        /// <param name="filedName">当前解析的字段名，主要用于日志打印，别无他用</param>
        /// <returns></returns>
        internal static String chaijieGSByJS(String gsnr, String filedName)
        {
            String valResult = "";
          //  LogPrinter.LogPrinter.printModDebugLog2File("即将解析字段(显示)" + filedName, "GSJieXi");
            String gsNewStr = gsnr;
            {
               // LogPrinter.LogPrinter.printModDebugLog2File("即将截取字段！", "GSJieXi");
                //第一步拆解出公式中的字段值，并且替换。
                List<String> fileList = substringFiled(gsNewStr, "[F&", "&F]");
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段(显示)" + filedName + ",查找字段值完成，即将拆解公式列表！", "GSJieXi");
                //拆分公式列表 
                List<String> gsList = new List<string>();
                List<String> list_gs_tmp = DefinedFormulaUtils.spliter(gsNewStr, "&V];[F&", "&V]", "[F&");
                foreach (String str in list_gs_tmp)
                {
                    foreach (String str_tmp in DefinedFormulaUtils.spliter(str, "&V];:[V&", "&V]", ":[V&"))
                    {
                      //  LogPrinter.LogPrinter.printModDebugLog2File("解析字段(显示)" + filedName + "拆解公式列表返回：" + str_tmp, "GSJieXi");
                        gsList.Add(str_tmp);
                    }
                }
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段(显示)" + filedName + ",拆解公式列表完成,列表长度："+gsList.Count, "GSJieXi");
                StringBuilder sbForJs = new StringBuilder();
                //拆解替换公式列表信息
                for (int i = 0; i < gsList.Count; i++)
                {
                    String gsTmp = gsList[i];
                    //替换条件
                    gsTmp = gsTmp.Replace("[T&=&T]", "==");
                    gsTmp = gsTmp.Replace("[T&>&T]", ">");
                    gsTmp = gsTmp.Replace("[T&>=&T]", ">=");
                    gsTmp = gsTmp.Replace("[T&<&T]", "<");
                    gsTmp = gsTmp.Replace("[T&<=&T]", "<=");
                    gsTmp = gsTmp.Replace("[T&<>&T]", "!=");
                    while (gsTmp.Contains("[T&startWith&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&startWith&T]"));
                        //添加尾部括号
                        gsTmp =  gsTmp.Substring(0, indexCondition) + "&C])==0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&startWith&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&startWith&T]".Length);
                    }
                    while (gsTmp.Contains("[T&contains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&contains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])>=0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&contains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&contains&T]".Length);
                    }
                    while (gsTmp.Contains("[T&notContains&T]"))
                    {
                        int indexCondition = gsTmp.IndexOf("&C]", gsTmp.IndexOf("[T&notContains&T]"));
                        //添加尾部括号
                        gsTmp = gsTmp.Substring(0, indexCondition) + "&C])<0" + gsTmp.Substring(indexCondition + 3);
                        //替换公式
                        indexCondition = gsTmp.IndexOf("[T&notContains&T]");
                        gsTmp = gsTmp.Substring(0, indexCondition) + ".indexOf(" + gsTmp.Substring(indexCondition + "[T&notContains&T]".Length);
                    }
                    //替换长度内容
                    gsTmp = gsTmp.Replace("[T&length>=&T]", ".length >= ");
                    gsTmp = gsTmp.Replace("[T&length<=&T]", ".length <= ");
                    gsTmp = gsTmp.Replace("[T&length!=&T]", ".length != ");
                    //替换值内容
                    gsTmp = gsTmp.Replace("[C&", "");
                    gsTmp = gsTmp.Replace("&C]", "");
                    //替换结果值前缀
                    gsTmp = gsTmp.Replace("&L]:[V&", "&L]){[V&");
                    //替换逻辑条件
                    gsTmp = gsTmp.Replace("[L&)and(&L]", ") && (");
                    gsTmp = gsTmp.Replace("[L&)and&L]", ") && ");
                    gsTmp = gsTmp.Replace("[L&and(&L]", " && (");
                    gsTmp = gsTmp.Replace("[L&and&L]", " && ");
                    gsTmp = gsTmp.Replace("[L&)or(&L]", ") || (");
                    gsTmp = gsTmp.Replace("[L&)or&L]", ") || ");
                    gsTmp = gsTmp.Replace("[L&or(&L]", " || (");
                    gsTmp = gsTmp.Replace("[L&or&L]", " || ");
                    gsTmp = gsTmp.Replace("[L&)&L]", " ) ");
                    gsTmp = gsTmp.Replace("[L&&L]", " ");
                    //如果只有结果则直接添加if true
                    gsTmp = gsTmp.Replace(":[V&", "true){[V&");
                    //替换结果字符
                    gsTmp = gsTmp.Replace("[V&", " ");
                    gsTmp = gsTmp.Replace("&V]", " ");
                    //添加至整体字符串 
                    sbForJs.Append((sbForJs.Length <= 0 ? "" : " else ") + "if(" + gsTmp + " }");
                }
                //最后替换系统字段   
                sbForJs.Replace("[F&", "");
                sbForJs.Replace("&F]", ""); 
               // LogPrinter.LogPrinter.printModDebugLog2File("解析字段(显示)" + filedName + ",替换及组合公式列表成js代码完成！", "GSJieXi");
                LogPrinter.LogPrinter.printModDebugLog2File("解析(显示)新公式fileGSNew，公式原值：" + gsNewStr + ",解析后：" + sbForJs.ToString(), "GSJieXi");
                return sbForJs.ToString();
            }
            //LogPrinter.LogPrinter.printModDebugLog2File("完成(显示)解析字段：" + filedName, "GSJieXi");
            return valResult;
        }

    }
}
